$(function () {
    // 默认加载部门树
    vm.getDept();
});

var ztree;
//部门树
var deptSetting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "deptId",
            pIdKey: "parentId",
            rootPId: '01'
        },
        key: {
            url: "nourl",
            name: "name" //name 与 typeName 绑定
        },
    },
    callback: {
        beforeCheck: true,
        beforeClick: outTreeCheck
    }
};

function outTreeCheck(e, treeObj, treeNode) {
    if(treeObj.deptLevel == 1){
        treeObj.checked = false;
        return false;
    }
    vm.q.lineCode = treeObj.dcode;
    vm.search();
}

var myChart;
var vm = new Vue({
    el: '#rrapp',
    data: {
        q:{
            month:'', // 当前月份
            lineCode:''
        },
        curDeptName:'',
        option : {
            title: {
                text: '区域设备故障次数统计',
                left: 'center',
                padding: 10
            },
            tooltip: {
                trigger: 'axis',
                axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                    type: 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
                }
            },
            color: ['#3398DB'],
            xAxis: [
                {
                    type: 'category',
                    data: [],
                    axisTick: {
                        alignWithLabel: true
                    },
                    minInterval:1,
                    axisLabel: {
                        interval:0,
                        rotate:-45
                    }
                }
            ],
            yAxis: [{type: 'value',minInterval: 1}],
            series: [
                {
                    name: '故障次数',
                    type: 'bar',
                    barWidth: '60%',
                    data: []
                }
            ]
        }
    },
    methods: {
        getDept: function () {
            //加载部门树
            Ajax.request({
                url: '../sys/user/treeDeptBySCXLevel?level=3',
                async: false,
                successCallback: function (r) {
                    ztree = $.fn.zTree.init($("#deptTree"), deptSetting, r.list);
                    ztree.expandAll(true);
                    var node = ztree.getNodeByParam("deptId", '0111');
                    if (node != null) {
                        vm.q.lineCode = node.dcode;
                        ztree.selectNode(node);
                        ztree.expandNode(node)
                    }
                }
            });
        },
        search: function () {
            var param = {};
            if(this.q.month){
                param.month = this.convertGMT2Month(this.q.month);
            }
            if(this.q.lineCode){
                param.dcode = this.q.lineCode;
            }
            myChart.showLoading();
            Ajax.request({
                url: "../device/stat/areaFaultTime",
                params: param,
                type: "POST",
                successCallback: function (res) {
                    myChart.hideLoading();
                    myChart.setOption({
                        xAxis: {
                            data: res.result.xaxisData
                        },
                        series: [{
                            data: res.result.barSeriesData[0].data
                        }]
                    });
                }
            });
        },
        resetSearch: function () {
            vm.q.month = '';
            vm.q.lineCode = '';
            vm.search();
        },
        convertGMT2Month: function(time){
            var date = new Date(time)
            var month = (date.getMonth() + 1);
            return date.getFullYear() + '-' + (month < 10? '0':'') + month;
        }
    },
    mounted: function(){
        // 初始化
        myChart = echarts.init(document.getElementById("container"));
        myChart.setOption(this.option,true);
        this.search();
    }
});
